﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Schools;
using PH.Logic.Schools;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("SchoolInfo")]
    public class SchoolInfoApiController : ApiController
    {
        [Route("GetSchoolList")]
        [HttpPost]
        public JsonEntitySet<SchoolModel> GetSchoolList(JObject request)
        {
            string errinfo = "no data";
            try
            {
                SchoolQuery search = new SchoolQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                search.Key = request.Value<string>("key");
                //search.BureauID = request.Value<int>("bureauID");
                search.OwnerID = Global.WebContext.CurrentUser.AssociatedBureauID.Value;
                search.GetUser = request.Value<bool>("getUser");
                search.GetPersonCount = request.Value<bool>("getPersonCount");
                search.GetSchoolType = request.Value<bool>("getSchoolType");
                var result = SchoolLogic.GetSchools(search);
                return new JsonEntitySet<SchoolModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("",ex);
            }
            return new JsonEntitySet<SchoolModel>(null, errinfo, false);
        }
        /// <summary>
        /// 查询当前机构下的所有学校信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("GetSchools")]
        [HttpPost]
        public JsonEntitySet<SchoolModel> GetSchools(JObject request)
        {
            string errinfo = "no data";
            try
            {
                SchoolQuery search = new SchoolQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                search.Key = request.Value<string>("key");
                search.GetUser = request.Value<bool>("getUser");
                search.BureauID = request.Value<int>("bureauID");// Global.WebContext.CurrentUser.AssociatedBureauID.Value;
                search.GetSchoolType = request.Value<bool>("getSchoolType");
                search.GetPersonCount = request.Value<bool>("getPersonCount");
                var result = SchoolLogic.GetSchools(search);
                return new JsonEntitySet<SchoolModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<SchoolModel>(null, errinfo, false);
        }

        [Route("SaveSchool")]
        [HttpPost]
        public JsonEntity<SchoolModel> SaveSchool(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                SchoolModel school = new SchoolModel();
                var guid = request.Value<string>("guid");
                school.Guid = new Guid(guid);
                school.SchoolName = request.Value<string>("schoolName");
                school.SchType = request.Value<int>("schType");
                var oGuid = request.Value<string>("ownerGuid");
                if (!string.IsNullOrEmpty(oGuid))
                    school.OwnerGuid = new Guid(oGuid);
                school.Code = request.Value<string>("code");
                #endregion

                if (school.Guid==Guid.Empty)
                    school = SchoolLogic.CreateNewSchool(school);
                else
                    SchoolLogic.UpdateSchool(school);
                return new JsonEntity<SchoolModel>(school);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("",ex);
            }
            return new JsonEntity<SchoolModel>(null, errinfo, false);
        }

        [Route("DeleteSchool")]
        [HttpPost]
        public JsonResult<bool> DeleteSchool(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                {
                    SchoolLogic.DeleteSchool(new Guid(guid));
                    return new JsonResult<bool>(true);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("Remove")]
        [HttpPost]
        public JsonResult<bool> Remove(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                {
                    SchoolLogic.Remove(new Guid(guid));
                    return new JsonResult<bool>(true);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
